using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using DTO;
using System.Data;
using System.Collections;

namespace DAOLayer
{
    public class SachMatDao
    {
        public void ThemSachMat(SachMatDto sm)
        {
            OleDbConnection cn = Database.ConnectionData();
            string strSQL = "Insert into SachMat (MaCuonSach,MaDocGia,NgayGhiNhan,NguoiGhiNhan,TienPhat) values(?,?,?,?,?)";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            
            cmd.Parameters.Add("@MaCuonSach", OleDbType.WChar);
            cmd.Parameters["@MaCuonSach"].Value = sm.MaCuonSach;

            cmd.Parameters.Add("@MaDocGia", OleDbType.WChar);
            cmd.Parameters["@MaDocGia"].Value = sm.MaDocGia;

            cmd.Parameters.Add("@NgayGhiNhan", OleDbType.Date);
            cmd.Parameters["@NgayGhiNhan"].Value = sm.NgayGhiNhan;

            cmd.Parameters.Add("@NguoiGhiNhan", OleDbType.WChar);
            cmd.Parameters["@NguoiGhiNhan"].Value = sm.NguoiGhiNhan;

            cmd.Parameters.Add("@TienPhat", OleDbType.Double);
            cmd.Parameters["@TienPhat"].Value = sm.TienPhat;

            cmd.ExecuteNonQuery();
            cn.Close();
        }

        public void CapNhatBangSachMat(DataTable dt)
        {
            OleDbConnection cn;
            cn = Database.ConnectionData();
            string strSQL;
            strSQL = "Select * From SachMat";
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(strSQL, cn);
            OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
            da.Update(dt);
            cn.Close();
        }


        public void CapNhatSachMat(SachMatDto sm)
        {

            OleDbConnection cn = Database.ConnectionData();
            string strSQL;
            strSQL = "Update SachMat Set MaCuonSach = ? and MaDocGia = ? and NgayGhiNhan = ? and NguoiGhiNhan = ? and TienPhat";
            OleDbCommand cmd = new OleDbCommand(strSQL, cn);
            cmd.Parameters.Add("@MaCuonsach", OleDbType.WChar);
            cmd.Parameters.Add("@MaDocGia", OleDbType.WChar);
            cmd.Parameters.Add("@NgayGhiNhan", OleDbType.Date);
            cmd.Parameters.Add("@NguoiGhiNhan", OleDbType.WChar);
            cmd.Parameters.Add("@TienPhat", OleDbType.Double);

            cmd.Parameters["@MaCuonsach"].Value = sm.MaCuonSach;
            cmd.Parameters["@MaDocGia"].Value = sm.MaDocGia;
            cmd.Parameters["@NgayGhiNhan"].Value = sm.NgayGhiNhan;
            cmd.Parameters["@NguoiGhiNhan"].Value = sm.NguoiGhiNhan;
            cmd.Parameters["@TienPhat"].Value = sm.TienPhat;

            cmd.ExecuteNonQuery();
            cn.Close();

        }

        public DataTable LayBangSachMat()
        {
            DataTable dt = new DataTable();
            OleDbConnection cn = Database.ConnectionData();
            string strSQL = "Select * From SachMat";
            OleDbDataAdapter da;
            da = new OleDbDataAdapter(strSQL, cn);
            da.Fill(dt);
            cn.Close();
            return dt;
        }
    }
}
